package com.csii.mobilebanking;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.csii.mobilebanking.Strings.Extra;

public class FrameContent extends Activity implements OnItemClickListener,OnClickListener{
	
	private TextView contentTextView,contentTitle,frameTitle,headTextView;
	private ListView moreListView;
	private View footerView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.frame_content_layout);
		loadView();
		setListViewAdapter();

		
	}

	private void setListViewAdapter() {
		frameTitle.setText(getIntent().getIntExtra(Extra.LSIT_TITLE_KEY, 0));
		if(getIntent().getStringArrayExtra(Extra.MORE_CONTENT_KEY)!=null){
			List<HashMap<String,String>> dataList=new ArrayList<HashMap<String,String>>();
			String[] content=getIntent().getStringArrayExtra(Extra.MORE_CONTENT_KEY);
			String[] title=getIntent().getStringArrayExtra(Extra.MORE_TITLE_KEY);
			String[] date=getIntent().getStringArrayExtra(Extra.MORE_DATE_KEY);
			for(int i=0;i<content.length;i++){
				HashMap<String,String> map=new HashMap<String,String>();
				map.put(Extra.MORE_CONTENT_KEY, content[i]);
				map.put(Extra.MORE_TITLE_KEY, title[i]);
				map.put(Extra.MORE_DATE_KEY, date[i]);
				dataList.add(map);
			}
			//moreListView.addFooterView(v)
			moreListView.removeFooterView(footerView);
			headTextView.setText("共有 "+content.length+" 条信息");
			moreListView.setAdapter(new MoreListViewAdapter(this, dataList, R.layout.item_morelistview, new String[]{Extra.MORE_TITLE_KEY,Extra.MORE_DATE_KEY}, 
															new int[]{R.id.text0,R.id.text1}));

			contentTitle.setText(dataList.get(0).get(Extra.MORE_TITLE_KEY));
			contentTextView.setText(dataList.get(0).get(Extra.MORE_CONTENT_KEY));
		}else{
			List<HashMap<String,String>> dataList=new ArrayList<HashMap<String,String>>();
			String content=getIntent().getStringExtra(Extra.MORE_CONTENT_KEY);
			String title=getIntent().getStringExtra(Extra.MORE_TITLE_KEY);
			String date=getIntent().getStringExtra(Extra.MORE_DATE_KEY);
			HashMap<String,String> map=new HashMap<String,String>();
			map.put(Extra.MORE_CONTENT_KEY, content);
			map.put(Extra.MORE_TITLE_KEY, title);
			map.put(Extra.MORE_DATE_KEY, date);
			dataList.add(map);

			
			headTextView.setText("共有 1 条信息");
			moreListView.setAdapter(new MoreListViewAdapter(this, dataList, R.layout.item_morelistview, new String[]{Extra.MORE_TITLE_KEY,Extra.MORE_DATE_KEY}, 
															new int[]{R.id.text0,R.id.text1}));

			contentTitle.setText(dataList.get(0).get(Extra.MORE_TITLE_KEY));
			contentTextView.setText(dataList.get(0).get(Extra.MORE_CONTENT_KEY));
		
		}
		
	}
	private void loadingAll(){
		List<HashMap<String,String>> dataList=new ArrayList<HashMap<String,String>>();
		String[] content = null;
		String[] title=null;
		String[] date=null;
		int moreType=getIntent().getIntExtra(Extra.LSIT_TITLE_KEY, 0);
		switch(moreType){
		case R.string.frame_title0:
			content=Strings.MORE_MSG_CONTENT;
			title=Strings.MORE_MSG_TITLE;
			date=Strings.MORE_MSG_DATE;
			break;
		case R.string.frame_title1:
			content=Strings.MORE_ANN_CONTENT;
			title=Strings.MORE_ANN_TITLE;
			date=Strings.MORE_ANN_DATE;
			break;
		case 0:
			return;
		}
		if(moreType!=0){
			for(int i=0;i<content.length;i++){
				HashMap<String,String> map=new HashMap<String,String>();
				map.put(Extra.MORE_CONTENT_KEY, content[i]);
				map.put(Extra.MORE_TITLE_KEY, title[i]);
				map.put(Extra.MORE_DATE_KEY,date[i]);
				dataList.add(map);
			}
			//moreListView.addFooterView(v)
			moreListView.removeFooterView(footerView);
			headTextView.setText("共有 "+content.length+" 条信息");
			moreListView.setAdapter(new MoreListViewAdapter(this, dataList, R.layout.item_morelistview, new String[]{Extra.MORE_TITLE_KEY,Extra.MORE_DATE_KEY}, 
															new int[]{R.id.text0,R.id.text1}));

			contentTitle.setText(dataList.get(0).get(Extra.MORE_TITLE_KEY));
			contentTextView.setText(dataList.get(0).get(Extra.MORE_CONTENT_KEY));
		}
	}
	
	private void loadView() {
		frameTitle=(TextView)findViewById(R.id.frame_title);
		contentTextView=(TextView)findViewById(R.id.frame_textview);
		contentTitle=(TextView)findViewById(R.id.content_title);
		View headView=getLayoutInflater().inflate(R.layout.left_headerview, null);
		footerView=getLayoutInflater().inflate(R.layout.frame_more_btn, null);
		headTextView=(TextView)headView.findViewById(R.id.left_count);
		moreListView=(ListView)findViewById(R.id.frame_listview);
		((Button)findViewById(R.id.title_bak)).setOnClickListener(this);
		((Button)footerView.findViewById(R.id.footer_more_btn)).setOnClickListener(this);
		moreListView.setOnItemClickListener(this);
		moreListView.addHeaderView(headView);
		moreListView.addFooterView(footerView);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onBackPressed() {
		finish();
	}

	@Override
	public void onItemClick(AdapterView<?> adapterview, View arg1, int position, long arg3) {
		if(position>0){
			@SuppressWarnings("unchecked")
			HashMap<String,String> map=(HashMap<String, String>)adapterview.getItemAtPosition(position);
			contentTitle.setText(map.get(Extra.MORE_TITLE_KEY));
			contentTextView.setText(map.get(Extra.MORE_CONTENT_KEY));
		}
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.title_bak:
			finish();
			break;
		case R.id.footer_more_btn:
			loadingAll();
			break;
		}
		
	}
	

}
